package com.hksj.Intelligentmanagement.auth.controller;

import com.hksj.Intelligentmanagement.auth.dto.MenuDTO;
import com.hksj.Intelligentmanagement.auth.dto.MenuStatusDTO;
import com.hksj.Intelligentmanagement.auth.service.MenuService;
import com.hksj.Intelligentmanagement.auth.vo.MenuVo;
import com.hksj.Intelligentmanagement.common.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜单权限管理")
@RestController
@RequestMapping("/api/menu")
public class MenuController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	public MenuService menuService;

	@PostMapping("/createMenu")
	@ApiOperation(value = "添加菜单")
	public Rest createMenu(@RequestBody @Validated(MenuDTO.Create.class)  MenuDTO menu){
		menuService.createMenu(menu);
		return Rest.ok();
	}

	@PostMapping("/updateMenu")
	@ApiOperation(value = "修改菜单")
	public Rest updateMenu(@RequestBody @Validated(MenuDTO.Update.class)  MenuDTO menu){
		menuService.updateMenu(menu);
		return Rest.ok();
	}

	@GetMapping("/deleteMenu/{id}")
	@ApiOperation(value = "删除菜单表", notes = "删除菜单表")
	public Rest deleteMenu(@PathVariable("id") String id){
		menuService.deleteMenu(id);
		return Rest.ok();
	}

	@ApiOperation(value = "查询用户菜单权限树")
	@PostMapping("/getUserMenuTree")
	public Rest<List<MenuVo>> getUserMenuTree(
		@ApiParam(value = "用户id",required = true) @RequestParam(required = true) String userId){
		List<MenuVo> userMenuTree = menuService.getUserMenuTree(userId);
		return Rest.ok(userMenuTree);
	}

	@ApiOperation(value = "查询用户权限在总权限树中的选中状态")
	@PostMapping("/getUserMenuTreeInAll")
	public Rest<List<MenuVo>> getUserMenuTreeInAll(
		@ApiParam(value = "用户id",required = true) @RequestParam(required = true) String userId){
		List<MenuVo> userMenuTree = menuService.getUserMenuTreeInAll(userId);
		return Rest.ok(userMenuTree);
	}

	@ApiOperation(value = "查询总菜单权限树")
	@PostMapping("/getAllMenuTree")
	public Rest<List<MenuVo>> getAllMenuTree(
		@ApiParam(value = "菜单状态  = 0 查所有启用的 ,不传查所有（包含禁用状态的）",required = false) @RequestParam(required = false) String status){
		List<MenuVo> userMenuTree = menuService.getAllMenuTree(status);
		return Rest.ok(userMenuTree);
	}

	@ApiOperation(value = "禁用/启用菜单")
	@PostMapping("/switchMenu")
	public Rest switchMenu(
		@ApiParam(value = "禁用/启用有子菜单的菜单时，子菜单id一起传",required = true)MenuStatusDTO menuStatus){
		menuService.switchMenu(menuStatus);
		return Rest.ok();
	}
}